<template>
  <div class="screen-config-page">
    <div class="page-header">
      <h2>大屏配置管理</h2>
    </div>

    <!-- 标题配置 -->
    <div class="config-section">
      <h3>平台标题设置</h3>
      <el-form
        ref="titleForm"
        :model="titleForm"
        :rules="titleRules"
        label-width="120px"
        style="max-width: 600px;"
      >
        <el-form-item label="平台标题" prop="title">
          <el-input
            v-model="titleForm.title"
            placeholder="请输入平台标题"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="updateTitle"
            :loading="submitLoading"
          >
            {{ submitLoading ? '保存中...' : '保存标题' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 预览区域 -->
    <div class="preview-section" v-if="titleForm.title">
      <h3>标题预览</h3>
      <div class="title-preview">
        {{ titleForm.title }}
      </div>
    </div>
  </div>
</template>

<script>
import { getPlatformTitleAPI, updatePlatformTitleAPI } from '@/api/admin';

export default {
  name: 'ScreenConfig',
  data() {
    return {
      submitLoading: false,
      // 标题表单
      titleForm: {
        title: ''
      },
      titleRules: {
        title: [
          { required: true, message: '请输入平台标题', trigger: 'blur' },
          { min: 1, max: 50, message: '标题长度不能超过50个字符', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    // 页面加载时获取当前标题
    this.getCurrentTitle();
  },
  methods: {
    // 获取当前标题
    getCurrentTitle() {
      getPlatformTitleAPI().then(res => {
        if (res.code === 200) {
          this.titleForm.title = res.data.title || '';
        } else {
          this.$message.error(res.message || '获取标题失败');
        }
      }).catch(error => {
        console.error('获取标题错误:', error);
        this.$message.error('获取标题失败，请检查网络');
      });
    },
    
    // 更新标题
    updateTitle() {
      this.$refs.titleForm.validate((valid) => {
        if (valid) {
          this.submitLoading = true;
          const data = {
            title: this.titleForm.title
          };
          
          updatePlatformTitleAPI(data).then(res => {
            this.submitLoading = false;
            if (res.code === 200) {
              this.$message.success('标题更新成功');
              // 可以考虑通知其他组件标题已更新
              this.$emit('title-updated', this.titleForm.title);
            } else {
              this.$message.error(res.message || '更新失败');
            }
          }).catch(error => {
            this.submitLoading = false;
            this.$message.error('更新失败，请检查网络');
            console.error('更新标题错误:', error);
          });
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.screen-config-page {
  background: #fff;
  padding: 20px;
  border-radius: 4px;
  
  .page-header {
    margin-bottom: 30px;
    
    h2 {
      margin: 0;
      font-size: 20px;
      color: #333;
    }
  }
  
  .config-section {
    margin-bottom: 30px;
    
    h3 {
      margin: 0 0 20px;
      font-size: 18px;
      color: #333;
      padding-bottom: 10px;
      border-bottom: 1px solid #eee;
    }
  }
  
  .preview-section {
    h3 {
      margin: 0 0 20px;
      font-size: 18px;
      color: #333;
      padding-bottom: 10px;
      border-bottom: 1px solid #eee;
    }
    
    .title-preview {
      padding: 20px;
      background: #f5f7fa;
      border-radius: 4px;
      font-size: 24px;
      font-weight: bold;
      color: #333;
      text-align: center;
      min-height: 60px;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}

::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
}
</style>